<?php
namespace app\pay\controller;
use think\Input;
class Notify extends Common{
    public function initialize(){
        parent::initialize(); 
    }  
    // 异步通知
    public function index(){ 
    	if(request()->isPost()){
            $order=db('channel_order')->where(array('id' =>input('id'),'state' =>2,'points' =>0))->find();
            if($order){  
                //创建签名
                $requestarray = array(
                    "orderid" =>$order['orderid'],
                    "orderid_out" =>$order['orderid_out'],
                    "money" => $order['money'], 
                    "number" => $order['number'],  
                    "version" => $this->sys['version'],   
                ); 
                ksort($requestarray); //按照键名进行升序排序 
                $Md5key=db('shop')->where('id',$order['shopid'])->value('appkey');
                $md5str = "";
                foreach ($requestarray as $key => $val) {
                    $md5str = $md5str . $key . "=" . $val . "&";
                } 
                $sign = strtoupper(md5($md5str . "key=" . $Md5key));  //签名转大写
                $requestarray['sign']=$sign;
                $requestarray['orderid']=$order['orderid']; 
                $requestarray["addtime"] = $order['addtime']; 
                $requestarray["addtime"] = $order['addtime']; 
                $requestarray["paytime"] = $order['paytime']; 
                $requestarray["state"] = $order['state']; //0未支付，1已支付未确认，2已经确认
                $requestarray["points"] = $order['points']; //0未上分，1自动上分，2手动上分    仅state==2,points==0才可上分               
                if($sign){
                    $notifyurl=$order['notifyurl'];
                    $curl = curl_init();
                    curl_setopt($curl, CURLOPT_URL,$notifyurl);//设置抓取的url
                    curl_setopt($curl, CURLOPT_HEADER, 'Content-Type:application/x-www-form-urlencoded');//设置头文件的信息作为数据流输出
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回，而不是直接输出。
                    curl_setopt($curl, CURLOPT_POST, 1);//设置post方式提交
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $md5str."&sign=".$requestarray['sign']."&addtime=".$requestarray['addtime']."&paytime=".$requestarray['paytime']."&state=".$requestarray['state']."&points=".$requestarray['points']);
                    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
                    $res_data = curl_exec($curl);//执行命令
                    curl_close($curl);//关闭URL请求   
                    $res=json_decode($res_data,true);
                    if(isset($res['code'])){
                        if($res['code']='SUCCESS'){
                            //变更状态
                            if(db('channel_order')->where('orderid',$order['orderid'])->where('state',2)->update(['points'=>1])!=false){
                                $result = ['code'=>1,'msg'=>'上分成功!'];
                            }else{            
                                $result = ['code'=>-1,'msg'=>'上分失败，请重试!'.$res_data];
                            }  
                        }else{
                            $result = ['code'=>-1,'msg'=>'上分失败或签名错误!'];
                        } 
                    }else{
                        if($res_data=='SUCCESS'){
                            if(db('channel_order')->where('orderid',$order['orderid'])->where('state',2)->update(['points'=>1])!=false){
                                $result = ['code'=>1,'msg'=>$res_data];
                            }else{            
                                $result = ['code'=>-1,'msg'=>'上分失败，请重试!'.$res_data];
                            }   
                        }else{ 
                            $result = ['code'=>-1,'msg'=>$res_data];
                        } 
                    }
                }else{
                    $result = ['code'=>-1,'msg'=>'请创建签名!'];
                } 

            }else{
                $result = ['code'=>-1,'msg'=>'订单异常!'];
            } 
    	}else{
            $result = ['code'=>-1,'msg'=>'这样不好!'];
        }      
    	return json($result);
    }      
}  